package com.caigou.task;

import com.caigou.bean.cms.entity.Channel;
import com.caigou.component.HtmlBuildSubstationComponent;
import com.caigou.component.RedisComponent;
import com.caigou.mapper.cms.ChannelMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Slf4j
@Component
@EnableScheduling
public class SubstationTask {

    @Resource
    private RedisComponent redisComponent;
    @Resource
    private HtmlBuildSubstationComponent substationComponent;

    @Resource
    private ChannelMapper channelService;

    // 定时生成分站易找标碎片
    @Scheduled(cron = "0 0 0/1 * * ?")// 分钟
    public void disposeSubstation(){
        // 判别任务同步锁
        String key = "task:lock:disposeSubstation";// 旧数据同步锁的key
        boolean lockFlag = redisComponent.setLock(key, 3000);
        if (!lockFlag){
            log.info("旧数据同步任务锁获取失败");
            return;
        }
        log.info("开始执行分站易找标碎片生成任务");
        try {
            // 河北分站
            Channel hebeiChannel = channelService.loadChannelByPlate("201010103");
            substationComponent.buildSubstation(hebeiChannel,"130000","河北省", 1, "HEBEIMARK");
            // 河南分站
            Channel henanChannel = channelService.loadChannelByPlate("301010103");
            substationComponent.buildSubstation(henanChannel,"410000","河南省", 1, "HENANMARK");
            // 山东分站
            Channel shandongChannel = channelService.loadChannelByPlate("401010103");
            substationComponent.buildSubstation(shandongChannel,"370000","山东省", 1, "SHANDONGMARK");
            // 辽宁
            Channel liaoningChannel = channelService.loadChannelByPlate("601010103");
            substationComponent.buildSubstation(liaoningChannel,"210000","辽宁省", 1, "LIAONINGMARK");
            // 江苏
            Channel jiangsuChannel = channelService.loadChannelByPlate("901010103");
            substationComponent.buildSubstation(jiangsuChannel,"320000","江苏省", 1, "JIANGSUMARK");
            // 浙江
            Channel zhejiangChannel = channelService.loadChannelByPlate("1001010103");
            substationComponent.buildSubstation(zhejiangChannel,"330000","浙江省", 1, "ZHEJIANGMARK");
            // 福建
            Channel fujianChannel = channelService.loadChannelByPlate("1201010103");
            substationComponent.buildSubstation(fujianChannel,"350000","福建省", 1, "FUJIANMARK");
            // 湖北分站
            Channel hubeiChannel = channelService.loadChannelByPlate("1401010103");
            substationComponent.buildSubstation(hubeiChannel,"420000","湖北省", 1, "HUBEIMARK");
            // 海南
            Channel hainanChannel = channelService.loadChannelByPlate("1701010103");
            substationComponent.buildSubstation(hainanChannel,"460000","海南省", 1, "HAINANMARK");
            // 贵州
            Channel guizhouChannel = channelService.loadChannelByPlate("1901010103");
            substationComponent.buildSubstation(guizhouChannel,"520000","贵州省", 1, "GUIZHOUMARK");
            // 云南
            Channel yunnanChannel = channelService.loadChannelByPlate("2001010103");
            substationComponent.buildSubstation(yunnanChannel,"530000","云南省", 1, "YUNNANMARK");
            // 陕西
            Channel shaanxiChannel = channelService.loadChannelByPlate("2101010103");
            substationComponent.buildSubstation(shaanxiChannel,"610000","陕西省", 1, "SHAANXIMARK");
            // 北京
            Channel beijingChannel = channelService.loadChannelByPlate("2401010103");
            substationComponent.buildSubstation(beijingChannel,"110000","北京市", 1, "BEIJINGMARK");
            // 天津
            Channel tianjinChannel = channelService.loadChannelByPlate("2501010103");
            substationComponent.buildSubstation(tianjinChannel,"120000","天津市", 1, "TIANJINMARK");
            // 重庆
            Channel chongqingChannel = channelService.loadChannelByPlate("2601010103");
            substationComponent.buildSubstation(chongqingChannel,"500000","重庆市", 1, "CHONGQINGMARK");
            // 深圳
            Channel shenzhenChannel = channelService.loadChannelByPlate("3801010103");
            substationComponent.buildSubstation(shenzhenChannel,"440300","深圳市", 1, "SHENZHENMARK");
            // 青岛
            Channel qingdaoChannel = channelService.loadChannelByPlate("3501010103");
            substationComponent.buildSubstation(qingdaoChannel,"370200","青岛市", 1, "QINGDAOMARK");
            // 大连
            Channel dalianChannel = channelService.loadChannelByPlate("3401010103");
            substationComponent.buildSubstation(dalianChannel,"210200","大连市", 1, "DALIANMARK");
            // 西藏
            Channel xizangChannel = channelService.loadChannelByPlate("3201010103");
            substationComponent.buildSubstation(xizangChannel,"540000","西藏自治区", 1, "XIZANGMARK");
            // 新疆
            Channel xinjiangChannel = channelService.loadChannelByPlate("3101010103");
            substationComponent.buildSubstation(xinjiangChannel,"650000","新疆维吾尔自治区", 1, "XINJIANGMARK");
            // 宁夏
            Channel ningxiaChannel = channelService.loadChannelByPlate("3001010103");
            substationComponent.buildSubstation(ningxiaChannel,"640000","宁夏回族自治区", 1, "NINGXIAMARK");
            // 广西
            Channel guangxiChannel = channelService.loadChannelByPlate("2901010103");
            substationComponent.buildSubstation(guangxiChannel,"450000","广西壮族自治区", 1, "GUANGXIMARK");
            // 内蒙古
            Channel neimengguChannel = channelService.loadChannelByPlate("2801010102");
            substationComponent.buildSubstation(neimengguChannel,"150000","内蒙古自治区", 1, "NEIMENGGUMARK");
            // 甘肃
            Channel gansuChannel = channelService.loadChannelByPlate("2201010103");
            substationComponent.buildSubstation(gansuChannel,"620000","甘肃省", 1, "GANSUMARK");
            // 甘肃
            Channel bingtuanChannel = channelService.loadChannelByPlate("3301010103");
            substationComponent.buildSubstation(bingtuanChannel,"830000","新疆生产建设兵团", 1, "BINGTUANMARK");
        } catch (Exception e){
            log.error(e.getMessage(), e);
        }
        boolean releaseFlag = redisComponent.releaseLock(key);
        log.info("分站易找标碎片生成任务同步完成 释放锁"+releaseFlag);
    }
}
